package com.algorithm.onehundred2onehundredfifty;

/**
 * @author tuzhijin
 * @version Id: MaxProfit_121.java, v 0.1 2021/5/25 上午6:31 tuzhijin Exp $$
 */
public class MaxProfit_121 {

    public static void main(String[] args) {
        int[] init = {7, 3, 5, 3, 6, 4};
        System.out.println(maxProfit(init));
    }

    public static int maxProfit(int[] prices) {
        int maxProfit = 0, minPrice = Integer.MAX_VALUE;
        for (int i = 0; i < prices.length; i++) {
            // 寻找最小值
            if (prices[i] < minPrice) {
                minPrice = prices[i];
                // 当前值和最小值的差，找到最大差差值
            } else if (maxProfit < prices[i] - minPrice) {
                maxProfit = prices[i] - minPrice;
            }
        }
        return maxProfit;
    }

}
